package com.cstz.sms;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.apache.commons.codec.binary.Hex;
import org.apache.log4j.Logger;

import lombok.Data;

@Data
public class SubmitReq implements Serializable {

	private static final Logger log = Logger.getLogger(SubmitReq.class);
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String ecName="天津海河高科有限责任公司";
	private String apId="hhgk2";
	private String secretKey="hhgk123";
	private String templateId="4654318400504a2fab462128539b09c0";
	private String mobiles;
	private String params;
	private String sign="mK0YXmisi";
	private String addSerial="";
	private String mac;
	
	public void genMac(String code){
		StringBuilder sb = new StringBuilder();
		sb.append(ecName).append(apId).append(secretKey).append(templateId).append(mobiles).append("[\""+code+"\"]").append(sign).append(addSerial);
		
		log.error(sb.toString());
		mac = encryptToMD5(sb.toString());
	}
	
	public void genMac(){
		StringBuilder sb = new StringBuilder();
		sb.append(ecName).append(apId).append(secretKey).append(templateId).append(mobiles).append(params).append(sign).append(addSerial);
		
		try {
			
			mac = Hex.encodeHexString(sb.toString().getBytes("UTF-8"));
		} catch (UnsupportedEncodingException e) {
			
		}
	}
	
	// MD5转换
		public static String encryptToMD5(String password) {
			byte[] digesta = null;
			String result = null;
			try {

				// 得到一个MD5的消息摘要
				MessageDigest mdi = MessageDigest.getInstance("MD5");
				// 添加要进行计算摘要的信息
				mdi.update(password.getBytes("utf-8"));
				// 得到该摘要
				digesta = mdi.digest();
				result = byteToHex(digesta);
			} catch (NoSuchAlgorithmException e) {

			} catch (UnsupportedEncodingException e) {
				// TODO 自动生成
				e.printStackTrace();
			}
			return result;
		}

		/**
		 * 将二进制转化为16进制字符串
		 */
		public static String byteToHex(byte[] pwd) {
			StringBuilder hs = new StringBuilder("");
			String temp = "";
			for (int i = 0; i < pwd.length; i++) {
				temp = Integer.toHexString(pwd[i] & 0XFF);
				if (temp.length() == 1) {
					hs.append("0").append(temp);
				} else {
					hs.append(temp);
				}
			}
			return hs.toString().toLowerCase();
		}
}
